﻿using Newtonsoft.Json;
using SqlSugar;
using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace JT808Gateway.Data
{
    public class BaseRepository<TEntity> : IBaseRepository<TEntity> where TEntity : class, new()
    {
        private ISqlSugarClient _db
        {
            get
            {
                var _dbBase = GetDbBaseInstance();
                if (typeof(TEntity).GetTypeInfo().GetCustomAttributes(typeof(SugarTable), true).FirstOrDefault((x => x.GetType() == typeof(SugarTable))) is SugarTable sugarTable && !string.IsNullOrEmpty(sugarTable.TableDescription))
                {
                    _dbBase.ChangeDatabase(sugarTable.TableDescription.ToLower());
                }
                else
                {
                    _dbBase.ChangeDatabase(MainDb.CurrentDbConnId.ToLower());
                }
                return _dbBase;
            }
        }

        internal ISqlSugarClient Db
        {
            get { return _db; }
        }

        /// <summary>
        /// 开始事务
        /// </summary>
        /// <returns></returns>
        public IUnitOfWork BeginTran()
        {
            var _unitOfWork = new SugarUnitOfWork(this.Db);
            _unitOfWork.BeginTran();
            return _unitOfWork;
        }

        /// <summary>
        /// 获取SqlSugerClient连接对象
        /// </summary>
        /// <returns></returns>
        public SqlSugarClient GetDbBaseInstance()
        {
            var configPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Configs", "dbSettings.json");
            var fs = File.OpenRead(configPath);
            var sr = new StreamReader(fs);
            string dbConfig = sr.ReadToEnd();
            sr.Close(); fs.Close();

            if (string.IsNullOrWhiteSpace(dbConfig))
            {
                //LogHelper.Error($"请配置数据库连接DbConfig");
                return new SqlSugarClient(new ConnectionConfig());
            }
            var mutiDbList = JsonConvert.DeserializeObject<List<MutiDBOperate>>(dbConfig);
            MainDb.CurrentDbConnId = mutiDbList.Find(x => x.MainDB)?.ConnId;
            // 连接字符串
            var listConfig = new List<ConnectionConfig>();
            mutiDbList.ForEach(m =>
            {
                listConfig.Add(new ConnectionConfig()
                {
                    ConfigId = m.ConnId.ToLower(),
                    ConnectionString = m.Connection,
                    DbType = (SqlSugar.DbType)m.DbType,
                    IsAutoCloseConnection = true,
                    InitKeyType = InitKeyType.Attribute,
                    AopEvents = new AopEvents
                    {
                        OnLogExecuting = (sql, p) =>
                        {
                            //LogHelper.Debug(string.Join("\r\n", new string[] { GetParas(p), "【SQL语句】：" + sql }));
                            //LogHelper.Debug(string.Join("\r\n", new string[] { "--------", "【SQL语句】：" + GetWholeSql(p, sql) }));
                        }
                    },
                    MoreSettings = new ConnMoreSettings()
                    {
                        IsAutoRemoveDataCache = true
                    }
                }
               );
            });
            return new SqlSugarClient(listConfig);
        }

        private static string GetWholeSql(SugarParameter[] paramArr, string sql)
        {
            foreach (var param in paramArr)
            {
                sql.Replace(param.ParameterName, param.Value.ObjToString());
            }
            return sql;
        }

        private static string GetParas(SugarParameter[] pars)
        {
            string key = "【SQL参数】：";
            foreach (var param in pars)
            {
                key += $"{param.ParameterName}:{param.Value}\n";
            }
            return key;
        }

        /// <summary>
        /// 创建表达式
        /// </summary>
        /// <returns></returns>
        public Expression<Func<TEntity, bool>> CreateExpression()
        {
            Expression<Func<TEntity, bool>> exp = null;
            return exp;
        }

        #region 根据主键Id获取实体数据

        /// <summary>
        /// 根据主键Id获取实体数据
        /// </summary>
        /// <param name="tableName">表名称</param>
        /// <param name="objId">主键Id</param>
        /// <returns>返回表数据实体</returns>
        public TEntity GetById(object objId, string tableName = null)
        {
            if (!string.IsNullOrWhiteSpace(tableName))
            {
                return _db.Queryable<TEntity>().AS(tableName).InSingle(objId);
            }
            return _db.Queryable<TEntity>().InSingle(objId);
        }

        /// <summary>
        /// 根据主键Id获取实体数据
        /// </summary>
        /// <param name="tableName">表名称</param>
        /// <param name="objId">主键Id</param>
        /// <returns>返回表数据实体</returns>
        public async Task<TEntity> GetByIdAsync(object objId, string tableName = null)
        {
            if (!string.IsNullOrWhiteSpace(tableName))
            {
                return await _db.Queryable<TEntity>().AS(tableName).In(objId).SingleAsync();
            }
            return await _db.Queryable<TEntity>().In(objId).SingleAsync();
        }

        /// <summary>
        /// 根据主键Id获取实体数据
        /// </summary>
        /// <typeparam name="TResult">返回结果类型</typeparam>
        /// <param name="tableName">表名称</param>
        /// <param name="objId">主键Id</param>
        /// <returns>返回指定结构实体数据</returns>
        public TResult GetById<TResult>(object objId, string tableName = null)
        {
            if (!string.IsNullOrWhiteSpace(tableName))
            {
                return _db.Queryable<TEntity>().AS(tableName).In(objId).Select<TResult>().Single();
            }
            return _db.Queryable<TEntity>().In(objId).Select<TResult>().Single();
        }

        /// <summary>
        /// 根据主键Id获取实体数据
        /// </summary>
        /// <typeparam name="TResult">返回结果类型</typeparam>
        /// <param name="tableName">表名称</param>
        /// <param name="objId">主键Id</param>
        /// <returns>返回指定结构实体数据</returns>
        public async Task<TResult> GetByIdAsync<TResult>(object objId, string tableName = null)
        {
            if (!string.IsNullOrWhiteSpace(tableName))
            {
                return await _db.Queryable<TEntity>().AS(tableName).In(objId).Select<TResult>().SingleAsync();
            }
            return await _db.Queryable<TEntity>().In(objId).Select<TResult>().SingleAsync();
        }

        #endregion 根据主键Id获取实体数据

        #region 根据主键Id数组获取数据列表

        /// <summary>
        /// 根据ID数组查询数据
        /// </summary>
        /// <param name="lstIds">id列表（必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]），如果是联合主键，请使用Where条件</param>
        /// <param name="tableName">表名称</param>
        /// <returns>数据实体列表</returns>
        public List<TEntity> GetByIDs(int[] lstIds, string tableName = null)
        {
            if (!string.IsNullOrWhiteSpace(tableName))
            {
                return _db.Queryable<TEntity>().AS(tableName).In(lstIds).ToList();
            }
            return _db.Queryable<TEntity>().In(lstIds).ToList();
        }

        /// <summary>
        /// 根据ID数组查询数据
        /// </summary>
        /// <param name="lstIds">id列表（必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]），如果是联合主键，请使用Where条件</param>
        /// <param name="tableName">表名称</param>
        /// <returns>数据实体列表</returns>
        public async Task<List<TEntity>> GetByIDsAsync(int[] lstIds, string tableName = null)
        {
            if (!string.IsNullOrWhiteSpace(tableName))
            {
                return await _db.Queryable<TEntity>().AS(tableName).In(lstIds).ToListAsync();
            }
            return await _db.Queryable<TEntity>().In(lstIds).ToListAsync();
        }

        /// <summary>
        /// 根据ID数组查询数据
        /// </summary>
        /// <param name="lstIds">id列表（必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]），如果是联合主键，请使用Where条件</param>
        /// <param name="tableName">表名称</param>
        /// <returns>数据实体列表</returns>
        public List<TResult> GetByIDs<TResult>(int[] lstIds, string tableName = null)
        {
            if (!string.IsNullOrWhiteSpace(tableName))
            {
                return _db.Queryable<TEntity>().AS(tableName).In(lstIds).Select<TResult>().ToList();
            }
            return _db.Queryable<TEntity>().In(lstIds).Select<TResult>().ToList();
        }

        /// <summary>
        /// 根据ID数组查询数据
        /// </summary>
        /// <param name="lstIds">id列表（必须指定主键特性 [SugarColumn(IsPrimaryKey=true)]），如果是联合主键，请使用Where条件</param>
        /// <param name="tableName">表名称</param>
        /// <returns>数据实体列表</returns>
        public async Task<List<TResult>> GetByIDsAsync<TResult>(int[] lstIds, string tableName = null)
        {
            if (!string.IsNullOrWhiteSpace(tableName))
            {
                return await _db.Queryable<TEntity>().AS(tableName).In(lstIds).Select<TResult>().ToListAsync();
            }
            return await _db.Queryable<TEntity>().In(lstIds).Select<TResult>().ToListAsync();
        }

        #endregion 根据主键Id数组获取数据列表

        #region 单实体新增数据到db

        /// <summary>
        /// 写入实体数据
        /// </summary>
        /// <param name="tableName">表名称</param>
        /// <param name="entity">实体类</param>
        /// <returns></returns>
        public bool Insert(TEntity entity, string tableName = null)
        {
            if (!string.IsNullOrWhiteSpace(tableName))
            {
                var insert = _db.Insertable(entity).AS(tableName);
                return insert.ExecuteCommand() > 0;
            }
            var insert2 = _db.Insertable(entity);
            return insert2.ExecuteCommand() > 0;
        }

        /// <summary>
        /// 写入实体数据
        /// </summary>
        /// <param name="tableName">表名称</param>
        /// <param name="entity">实体类</param>
        /// <returns></returns>
        public async Task<bool> InsertAsync(TEntity entity, string tableName = null)
        {
            if (!string.IsNullOrWhiteSpace(tableName))
            {
                var insert = _db.Insertable(entity).AS(tableName);
                return await insert.ExecuteCommandAsync() > 0;
            }
            var insert2 = _db.Insertable(entity);
            return await insert2.ExecuteCommandAsync() > 0;
        }

        /// <summary>
        /// 写入实体数据 返回自增Id
        /// </summary>
        /// <param name="tableName">表名称</param>
        /// <param name="entity">实体类</param>
        /// <returns></returns>
        public int InsertReturnIdentity(TEntity entity, string tableName = null)
        {
            if (!string.IsNullOrWhiteSpace(tableName))
            {
                var insert = _db.Insertable(entity).AS(tableName);
                return insert.ExecuteReturnIdentity();
            }
            var insert2 = _db.Insertable(entity);
            return insert2.ExecuteReturnIdentity();
        }

        /// <summary>
        ///  写入实体数据 返回自增Id
        /// </summary>
        /// <param name="tableName">表名称</param>
        /// <param name="entity">实体类</param>
        /// <returns></returns>
        public async Task<int> InsertReturnIdentityAsync(TEntity entity, string tableName = null)
        {
            if (!string.IsNullOrWhiteSpace(tableName))
            {
                var insert = _db.Insertable(entity).AS(tableName);
                return await insert.ExecuteReturnIdentityAsync();
            }
            var insert2 = _db.Insertable(entity);
            return await insert2.ExecuteReturnIdentityAsync();
        }

        /// <summary>
        /// 写入实体数据
        /// </summary>
        /// <param name="entity">实体类</param>
        /// <param name="tableName">表名称</param>
        /// <param name="insertColumns">指定只插入列</param>
        /// <returns>返回自增量列</returns>
        public int InsertReturnIdentity(TEntity entity, Expression<Func<TEntity, object>> insertColumns = null)
        {
            var insert = _db.Insertable(entity);
            if (insertColumns == null)
            {
                return insert.ExecuteReturnIdentity();
            }
            else
            {
                return insert.InsertColumns(insertColumns).ExecuteReturnIdentity();
            }
        }

        /// <summary>
        /// 写入实体数据
        /// </summary>
        /// <param name="entity">实体类</param>
        /// <param name="tableName">表名称</param>
        /// <param name="insertColumns">指定只插入列</param>
        /// <returns>返回自增量列</returns>
        public int InsertReturnIdentity(string tableName, TEntity entity, Expression<Func<TEntity, object>> insertColumns = null)
        {
            var insert = _db.Insertable(entity).AS(tableName);
            if (insertColumns == null)
            {
                return insert.ExecuteReturnIdentity();
            }
            else
            {
                return insert.InsertColumns(insertColumns).ExecuteReturnIdentity();
            }
        }

        /// <summary>
        /// 写入实体数据
        /// </summary>
        /// <param name="entity">实体类</param>
        /// <param name="tableName">表名称</param>
        /// <param name="insertColumns">指定只插入列</param>
        /// <returns>返回自增量列</returns>
        public async Task<int> InsertReturnIdentityAsync(TEntity entity, Expression<Func<TEntity, object>> insertColumns = null)
        {
            var insert = _db.Insertable(entity);
            if (insertColumns == null)
            {
                return await insert.ExecuteReturnIdentityAsync();
            }
            else
            {
                return await insert.InsertColumns(insertColumns).ExecuteReturnIdentityAsync();
            }
        }

        /// <summary>
        /// 写入实体数据
        /// </summary>
        /// <param name="entity">实体类</param>
        /// <param name="tableName">表名称</param>
        /// <param name="insertColumns">指定只插入列</param>
        /// <returns>返回自增量列</returns>
        public async Task<int> InsertReturnIdentityAsync(string tableName, TEntity entity, Expression<Func<TEntity, object>> insertColumns = null)
        {
            var insert = _db.Insertable(entity).AS(tableName);
            if (insertColumns == null)
            {
                return await insert.ExecuteReturnIdentityAsync();
            }
            else
            {
                return await insert.InsertColumns(insertColumns).ExecuteReturnIdentityAsync();
            }
        }

        #endregion 单实体新增数据到db

        #region 单实体编辑数据到db

        /// <summary>
        /// 更新实体数据
        /// </summary>
        /// <param name="entity">实体类</param>
        /// <param name="tableName">表名称</param>
        /// <returns></returns>
        public bool Update(TEntity entity, string tableName = null)
        {
            //这种方式会以主键为条件
            if (!string.IsNullOrWhiteSpace(tableName))
            {
                return _db.Updateable(entity).AS(tableName).ExecuteCommandHasChange();
            }
            return _db.Updateable(entity).ExecuteCommandHasChange();
        }

        /// <summary>
        /// 更新实体数据
        /// </summary>
        /// <param name="entity">实体类</param>
        /// <returns></returns>
        public async Task<bool> UpdateAsync(TEntity entity, string tableName = null)
        {
            //这种方式会以主键为条件
            if (!string.IsNullOrWhiteSpace(tableName))
            {
                return await _db.Updateable(entity).AS(tableName).ExecuteCommandHasChangeAsync();
            }
            return await _db.Updateable(entity).ExecuteCommandHasChangeAsync();
        }

        /// <summary>
        /// 更新实体数据
        /// </summary>
        /// <param name="strSql">sql语句</param>
        /// <param name="parameters">参数</param>
        /// <returns></returns>
        public bool Update(string strSql, SugarParameter[] parameters = null)
        {
            return _db.Ado.ExecuteCommand(strSql, parameters) > 0;
        }

        /// <summary>
        /// 更新实体数据
        /// </summary>
        /// <param name="strSql">sql语句</param>
        /// <param name="parameters">参数</param>
        /// <returns></returns>
        public async Task<bool> UpdateAsync(string strSql, SugarParameter[] parameters = null)
        {
            return await _db.Ado.ExecuteCommandAsync(strSql, parameters) > 0;
        }

        /// <summary>
        /// 更新实体数据
        /// </summary>
        /// <param name="entity">实体类</param>
        /// <param name="lstColumns">更新的字段</param>
        /// <param name="lstIgnoreColumns">忽略的字段</param>
        /// <param name="strWhere">检索条件</param>
        /// <returns></returns>
        public bool Update(TEntity entity, List<string> lstColumns, List<string> lstIgnoreColumns = null, string strWhere = "")
        {
            var up = _db.Updateable(entity);
            if (lstIgnoreColumns != null && lstIgnoreColumns.Count > 0)
            {
                up = up.IgnoreColumns(lstIgnoreColumns.ToArray());
            }
            if (lstColumns != null && lstColumns.Count > 0)
            {
                up = up.UpdateColumns(lstColumns.ToArray());
            }
            if (!string.IsNullOrEmpty(strWhere))
            {
                up = up.Where(strWhere);
            }
            return up.ExecuteCommandHasChange();
        }

        /// <summary>
        /// 更新数据(lambda表达式，非实体)
        /// </summary>
        /// <param name="updateExpression">更新值表达式</param>
        /// <param name="whereExpression">检索表达式</param>
        public bool Update(Expression<Func<TEntity, bool>> updateExpression, Expression<Func<TEntity, bool>> whereExpression)
        {
            return Db.Updateable<TEntity>().SetColumns(updateExpression).Where(whereExpression).ExecuteCommandHasChange();
        }

        /// <summary>
        /// 更新实体数据
        /// </summary>
        /// <param name="entity">实体类</param>
        /// <param name="lstColumns">更新的字段</param>
        /// <param name="lstIgnoreColumns">忽略的字段</param>
        /// <param name="tableName">表名称</param>
        /// <param name="strWhere">检索条件</param>
        /// <returns></returns>
        public bool Update(string tableName, TEntity entity, List<string> lstColumns, List<string> lstIgnoreColumns = null, string strWhere = "")
        {
            var up = _db.Updateable(entity).AS(tableName);
            if (lstIgnoreColumns != null && lstIgnoreColumns.Count > 0)
            {
                up = up.IgnoreColumns(lstIgnoreColumns.ToArray());
            }
            if (lstColumns != null && lstColumns.Count > 0)
            {
                up = up.UpdateColumns(lstColumns.ToArray());
            }
            if (!string.IsNullOrEmpty(strWhere))
            {
                up = up.Where(strWhere);
            }
            return up.ExecuteCommandHasChange();
        }

        /// <summary>
        /// 更新实体数据
        /// </summary>
        /// <param name="entity">实体类</param>
        /// <param name="lstColumns">更新的字段</param>
        /// <param name="lstIgnoreColumns">忽略的字段</param>
        /// <param name="tableName">表名称</param>
        /// <param name="strWhere">检索条件</param>
        /// <returns></returns>
        public async Task<bool> UpdateAsync(TEntity entity, List<string> lstColumns, List<string> lstIgnoreColumns = null, string strWhere = "")
        {
            var up = _db.Updateable(entity);
            if (lstIgnoreColumns != null && lstIgnoreColumns.Count > 0)
            {
                up = up.IgnoreColumns(lstIgnoreColumns.ToArray());
            }
            if (lstColumns != null && lstColumns.Count > 0)
            {
                up = up.UpdateColumns(lstColumns.ToArray());
            }
            if (!string.IsNullOrEmpty(strWhere))
            {
                up = up.Where(strWhere);
            }
            return await up.ExecuteCommandHasChangeAsync();
        }

        /// <summary>
        /// 更新实体数据
        /// </summary>
        /// <param name="entity">实体类</param>
        /// <param name="lstColumns">更新的字段</param>
        /// <param name="lstIgnoreColumns">忽略的字段</param>
        /// <param name="tableName">表名称</param>
        /// <param name="strWhere">检索条件</param>
        /// <returns></returns>
        public async Task<bool> UpdateAsync(string tableName, TEntity entity, List<string> lstColumns, List<string> lstIgnoreColumns = null, string strWhere = "")
        {
            var up = _db.Updateable(entity).AS(tableName);
            if (lstIgnoreColumns != null && lstIgnoreColumns.Count > 0)
            {
                up = up.IgnoreColumns(lstIgnoreColumns.ToArray());
            }
            if (lstColumns != null && lstColumns.Count > 0)
            {
                up = up.UpdateColumns(lstColumns.ToArray());
            }
            if (!string.IsNullOrEmpty(strWhere))
            {
                up = up.Where(strWhere);
            }
            return await up.ExecuteCommandHasChangeAsync();
        }

        #endregion 单实体编辑数据到db

        #region 单实体保存数据到db

        /// <summary>
        /// 保存实体数据
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>操作结果</returns>
        public bool Save(TEntity entity)
        {
            return _db.Storageable(entity).ExecuteCommand() > 0;
        }

        /// <summary>
        /// 保存实体数据
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns>操作结果</returns>
        public async Task<bool> SaveAsync(TEntity entity)
        {
            await _db.Storageable(entity).ExecuteCommandAsync();
            return true;
        }

        #endregion 单实体保存数据到db

        #region 单实体删除

        /// <summary>
        /// 根据实体删除一条数据
        /// </summary>
        /// <param name="entity">实体类</param>
        /// <param name="tableName">表名称</param>
        /// <returns></returns>
        public bool Delete(TEntity entity, string tableName = null)
        {
            if (!string.IsNullOrWhiteSpace(tableName))
            {
                return _db.Deleteable(entity).AS(tableName).ExecuteCommandHasChange();
            }
            return _db.Deleteable(entity).ExecuteCommandHasChange();
        }

        /// <summary>
        /// 根据实体删除一条数据
        /// </summary>
        /// <param name="entity">实体类</param>
        /// <param name="tableName">表名称</param>
        /// <returns></returns>
        public async Task<bool> DeleteAsync(TEntity entity, string tableName = null)
        {
            if (!string.IsNullOrWhiteSpace(tableName))
            {
                return await _db.Deleteable(entity).AS(tableName).ExecuteCommandHasChangeAsync();
            }
            return await _db.Deleteable(entity).ExecuteCommandHasChangeAsync();
        }

        /// <summary>
        /// 删除指定ID的数据
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <param name="tableName">表名称</param>
        /// <returns></returns>
        public bool DeleteById(object id, string tableName = null)
        {
            if (!string.IsNullOrWhiteSpace(tableName))
            {
                return _db.Deleteable<TEntity>(id).AS(tableName).ExecuteCommandHasChange();
            }
            return _db.Deleteable<TEntity>(id).ExecuteCommandHasChange();
        }

        /// <summary>
        /// 删除指定ID的数据
        /// </summary>
        /// <param name="id">主键ID</param>
        /// <param name="tableName">表名称</param>
        /// <returns></returns>
        public async Task<bool> DeleteByIdAsync(object id, string tableName = null)
        {
            if (!string.IsNullOrWhiteSpace(tableName))
            {
                return await _db.Deleteable<TEntity>(id).AS(tableName).ExecuteCommandHasChangeAsync();
            }
            return await _db.Deleteable<TEntity>(id).ExecuteCommandHasChangeAsync();
        }

        /// <summary>
        /// 删除指定ID集合的数据(批量删除)
        /// </summary>
        /// <param name="ids">主键ID集合</param>
        /// <param name="tableName">表名称</param>
        /// <returns></returns>
        public bool DeleteByIds(object[] ids, string tableName = null)
        {
            if (!string.IsNullOrWhiteSpace(tableName))
            {
                return _db.Deleteable<TEntity>().AS(tableName).In(ids).ExecuteCommandHasChange();
            }
            return _db.Deleteable<TEntity>().In(ids).ExecuteCommandHasChange();
        }

        /// <summary>
        /// 删除指定ID集合的数据(批量删除)
        /// </summary>
        /// <param name="ids">主键ID集合</param>
        /// <param name="tableName">表名称</param>
        /// <returns></returns>
        public async Task<bool> DeleteByIdsAsync(object[] ids, string tableName = null)
        {
            if (!string.IsNullOrWhiteSpace(tableName))
            {
                return await _db.Deleteable<TEntity>().AS(tableName).In(ids).ExecuteCommandHasChangeAsync();
            }
            return await _db.Deleteable<TEntity>().In(ids).ExecuteCommandHasChangeAsync();
        }

        /// <summary>
        /// 删除指定属性匹配的数据
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="tableName">表名称</param>
        /// <returns></returns>
        public bool DeleteBy(Expression<Func<TEntity, bool>> expression, string tableName = null)
        {
            if (!string.IsNullOrWhiteSpace(tableName))
            {
                return _db.Deleteable<TEntity>().AS(tableName).Where(expression).ExecuteCommandHasChange();
            }
            return _db.Deleteable<TEntity>().Where(expression).ExecuteCommandHasChange();
        }

        /// <summary>
        /// 删除指定属性匹配的数据
        /// </summary>
        /// <param name="expression"></param>
        /// <param name="tableName">表名称</param>
        /// <returns></returns>
        public async Task<bool> DeleteByAsync(Expression<Func<TEntity, bool>> expression, string tableName = null)
        {
            if (!string.IsNullOrWhiteSpace(tableName))
            {
                return await _db.Deleteable<TEntity>().AS(tableName).Where(expression).ExecuteCommandHasChangeAsync();
            }
            return await _db.Deleteable<TEntity>().Where(expression).ExecuteCommandHasChangeAsync();
        }

        /// <summary>
        /// 事务-批量删除实体(速度快)
        /// </summary>
        /// <param name="listEntity">实体集合</param>
        /// <param name="tableName">表名称</param>
        /// <returns>影响行数</returns>
        public bool DeleteBatch(List<TEntity> listEntity, string tableName = null)
        {
            if (!string.IsNullOrWhiteSpace(tableName))
            {
                return _db.Deleteable(listEntity).AS(tableName).ExecuteCommand() > 0;
            }
            else
            {
                return _db.Deleteable(listEntity).ExecuteCommand() > 0;
            }
        }

        /// <summary>
        /// 事务-批量删除实体(速度快)
        /// </summary>
        /// <param name="listEntity">实体集合</param>
        /// <param name="tableName">表名称</param>
        /// <returns>影响行数</returns>
        public async Task<bool> DeleteBatchAsync(List<TEntity> listEntity, string tableName = null)
        {
            if (!string.IsNullOrWhiteSpace(tableName))
            {
                return await _db.Deleteable(listEntity).AS(tableName).ExecuteCommandAsync() > 0;
            }
            else
            {
                return await _db.Deleteable(listEntity).ExecuteCommandAsync() > 0;
            }
        }

        #endregion 单实体删除

        #region 批量新增数据到db

        /// <summary>
        /// 事务-批量插入实体(速度快)
        /// </summary>
        /// <param name="listEntity">实体集合</param>
        /// <param name="tableName">表名称</param>
        /// <returns>影响行数</returns>
        public bool InsertBatch(List<TEntity> listEntity, string tableName = null)
        {
            if (!string.IsNullOrWhiteSpace(tableName))
            {
                return _db.Insertable(listEntity).AS(tableName).ExecuteCommand() > 0;
            }
            else
            {
                return _db.Insertable(listEntity).ExecuteCommand() > 0;
            }
        }

        /// <summary>
        /// 事务-批量插入实体(速度快)
        /// </summary>
        /// <param name="listEntity">实体集合</param>
        /// <param name="tableName">表名称</param>
        /// <returns>影响行数</returns>
        public async Task<bool> InsertBatchAsync(List<TEntity> listEntity, string tableName = null)
        {
            if (!string.IsNullOrWhiteSpace(tableName))
            {
                return await _db.Insertable(listEntity).AS(tableName).ExecuteCommandAsync() > 0;
            }
            else
            {
                return await _db.Insertable(listEntity).ExecuteCommandAsync() > 0;
            }
        }

        /// <summary>
        /// 事务-批量插入实体(速度快)
        /// </summary>
        /// <param name="listEntity">实体集合</param>
        /// <param name="tableName">表名称</param>
        /// <returns>影响行数</returns>
        public bool InsertBatchTran(List<TEntity> listEntity, string tableName = null)
        {
            try
            {
                Db.Ado.BeginTran();
                if (!string.IsNullOrWhiteSpace(tableName))
                {
                    _db.Insertable(listEntity).AS(tableName).ExecuteCommand();
                }
                else
                {
                    _db.Insertable(listEntity).ExecuteCommand();
                }
                Db.Ado.CommitTran();
                return true;
            }
            catch (Exception ex)
            {
                Db.Ado.RollbackTran();
                return false;
            }
        }

        /// <summary>
        /// 事务-批量插入实体(速度快)
        /// </summary>
        /// <param name="listEntity">实体集合</param>
        /// <param name="tableName">表名称</param>
        /// <returns>影响行数</returns>
        public async Task<bool> InsertBatchTranAsync(List<TEntity> listEntity, string tableName = null)
        {
            try
            {
                Db.Ado.BeginTran();
                if (!string.IsNullOrWhiteSpace(tableName))
                {
                    await _db.Insertable(listEntity).AS(tableName).ExecuteCommandAsync();
                }
                else
                {
                    await _db.Insertable(listEntity).ExecuteCommandAsync();
                }
                Db.Ado.CommitTran();
                return true;
            }
            catch (Exception ex)
            {
                Db.Ado.RollbackTran();
                return false;
            }
        }

        #endregion 批量新增数据到db

        #region 批量编辑数据到db

        /// <summary>
        /// 批量更新实体(速度快)
        /// </summary>
        /// <param name="listEntity">实体集合</param>
        /// <param name="tableName">表名称</param>
        /// <returns>影响行数</returns>
        public bool UpdateBatch(List<TEntity> listEntity, string tableName = null)
        {
            if (!string.IsNullOrWhiteSpace(tableName))
            {
                return _db.Updateable(listEntity).AS(tableName).ExecuteCommandHasChange();
            }
            return _db.Updateable(listEntity).ExecuteCommandHasChange();
        }

        /// <summary>
        /// 批量更新实体(速度快)
        /// </summary>
        /// <param name="listEntity">实体集合</param>
        /// <param name="tableName">表名称</param>
        /// <returns>影响行数</returns>
        public async Task<bool> UpdateBatchAsync(List<TEntity> listEntity, string tableName = null)
        {
            if (!string.IsNullOrWhiteSpace(tableName))
            {
                return await _db.Updateable(listEntity).AS(tableName).ExecuteCommandHasChangeAsync();
            }
            return await _db.Updateable(listEntity).ExecuteCommandHasChangeAsync();
        }

        /// <summary>
        /// 事务-批量修改实体(速度快)
        /// </summary>
        /// <param name="listEntity">实体集合</param>
        /// <param name="tableName">表名称</param>
        /// <returns>影响行数</returns>
        public bool UpdateBatchTran(List<TEntity> listEntity, string tableName = null)
        {
            try
            {
                Db.Ado.BeginTran();
                if (!string.IsNullOrWhiteSpace(tableName))
                {
                    _db.Updateable(listEntity).AS(tableName).ExecuteCommand();
                }
                else
                {
                    _db.Updateable(listEntity).ExecuteCommand();
                }
                Db.Ado.CommitTran();
                return true;
            }
            catch (Exception ex)
            {
                Db.Ado.RollbackTran();
                return false;
            }
        }

        /// <summary>
        /// 事务-批量插入实体(速度快)
        /// </summary>
        /// <param name="listEntity">实体集合</param>
        /// <param name="tableName">表名称</param>
        /// <returns>影响行数</returns>
        public async Task<bool> UpdateBatchTranAsync(List<TEntity> listEntity, string tableName = null)
        {
            try
            {
                Db.Ado.BeginTran();
                if (!string.IsNullOrWhiteSpace(tableName))
                {
                    await _db.Updateable(listEntity).AS(tableName).ExecuteCommandAsync();
                }
                else
                {
                    await _db.Updateable(listEntity).ExecuteCommandAsync();
                }
                Db.Ado.CommitTran();
                return true;
            }
            catch (Exception ex)
            {
                Db.Ado.RollbackTran();
                return false;
            }
        }

        #endregion 批量编辑数据到db

        #region 判断数据是否存在

        /// <summary>
        /// 查询数据是否存在
        /// </summary>
        /// <param name="whereExpression">检索表达式</param>
        /// <param name="tableName">表名称</param>
        /// <returns></returns>
        public bool GetExist(Expression<Func<TEntity, bool>> whereExpression, string tableName = null)
        {
            if (!string.IsNullOrWhiteSpace(tableName))
            {
                return _db.Queryable<TEntity>().AS(tableName).WhereIF(whereExpression != null, whereExpression).Any();
            }
            return _db.Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).Any();
        }

        /// <summary>
        /// 查询数据是否存在
        /// </summary>
        /// <param name="whereExpression">whereExpression</param>
        /// <param name="tableName">表名称</param>
        /// <returns></returns>
        public async Task<bool> GetExistAsync(Expression<Func<TEntity, bool>> whereExpression, string tableName = null)
        {
            if (!string.IsNullOrWhiteSpace(tableName))
            {
                return await _db.Queryable<TEntity>().AS(tableName).WhereIF(whereExpression != null, whereExpression).AnyAsync();
            }
            return await _db.Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).AnyAsync();
        }

        #endregion 判断数据是否存在

        #region 获取单个实体

        /// <summary>
        ///根据指定条件查询结果实体
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="whereExpression">条件表达式</param>
        /// <param name="strOrderByFileds">排序规则</param>
        /// <returns></returns>
        public TEntity QuerySingle(Expression<Func<TEntity, bool>> whereExpression = null, string strOrderByFileds = null)
        {
            return _db.Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).OrderByIF(strOrderByFileds != null, strOrderByFileds).First();
        }

        /// <summary>
        ///根据指定条件查询结果实体
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="whereExpression">条件表达式</param>
        /// <param name="strOrderByFileds">排序规则</param>
        /// <returns></returns>
        public async Task<TEntity> QuerySingleAsync(Expression<Func<TEntity, bool>> whereExpression, string strOrderByFileds = null)
        {
            return await _db.Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).OrderByIF(strOrderByFileds != null, strOrderByFileds).FirstAsync();
        }

        /// <summary>
        ///根据指定条件查询结果实体
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="tableName">表名称</param>
        /// <param name="whereExpression">条件表达式</param>
        /// <param name="strOrderByFileds">排序规则</param>
        /// <returns></returns>
        public TEntity QuerySingle(string tableName, Expression<Func<TEntity, bool>> whereExpression, string strOrderByFileds = null)
        {
            return _db.Queryable<TEntity>().AS(tableName).WhereIF(whereExpression != null, whereExpression).OrderByIF(strOrderByFileds != null, strOrderByFileds).First();
        }

        /// <summary>
        ///根据指定条件查询结果实体
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="tableName">表名称</param>
        /// <param name="whereExpression">条件表达式</param>
        /// <param name="strOrderByFileds">排序规则</param>
        /// <returns></returns>
        public async Task<TEntity> QuerySingleAsync(string tableName, Expression<Func<TEntity, bool>> whereExpression, string strOrderByFileds = null)
        {
            return await _db.Queryable<TEntity>().AS(tableName).WhereIF(whereExpression != null, whereExpression).OrderByIF(strOrderByFileds != null, strOrderByFileds).FirstAsync();
        }

        /// <summary>
        ///根据指定条件查询结果实体
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="tableName">表名称</param>
        /// <param name="whereExpression">条件表达式</param>
        /// <param name="strOrderByFileds">排序规则</param>
        /// <returns></returns>
        public TResult QuerySingle<TResult>(Expression<Func<TEntity, bool>> whereExpression = null, string strOrderByFileds = null)
        {
            return _db.Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).OrderByIF(strOrderByFileds != null, strOrderByFileds).Select<TResult>().First();
        }

        /// <summary>
        ///根据指定条件查询结果实体
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="tableName">表名称</param>
        /// <param name="whereExpression">条件表达式</param>
        /// <param name="strOrderByFileds">排序规则</param>
        /// <returns></returns>
        public async Task<TResult> QuerySingleAsync<TResult>(Expression<Func<TEntity, bool>> whereExpression, string strOrderByFileds = null)
        {
            return await _db.Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).OrderByIF(strOrderByFileds != null, strOrderByFileds).Select<TResult>().FirstAsync();
        }

        /// <summary>
        ///根据指定条件查询结果实体
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="tableName">表名称</param>
        /// <param name="whereExpression">条件表达式</param>
        /// <param name="strOrderByFileds">排序规则</param>
        /// <returns></returns>
        public TResult QuerySingle<TResult>(string tableName, Expression<Func<TEntity, bool>> whereExpression, string strOrderByFileds = null)
        {
            return _db.Queryable<TEntity>().AS(tableName).WhereIF(whereExpression != null, whereExpression).OrderByIF(strOrderByFileds != null, strOrderByFileds).Select<TResult>().First();
        }

        /// <summary>
        ///根据指定条件查询结果实体
        /// </summary>
        /// <typeparam name="TResult"></typeparam>
        /// <param name="tableName">表名称</param>
        /// <param name="whereExpression">条件表达式</param>
        /// <param name="strOrderByFileds">排序规则</param>
        /// <returns></returns>
        public async Task<TResult> QuerySingleAsync<TResult>(string tableName, Expression<Func<TEntity, bool>> whereExpression, string strOrderByFileds = null)
        {
            return await _db.Queryable<TEntity>().AS(tableName).WhereIF(whereExpression != null, whereExpression).OrderByIF(strOrderByFileds != null, strOrderByFileds).Select<TResult>().FirstAsync();
        }

        /// <summary>
        /// 根据sql语句查询
        /// </summary>
        /// <param name="strSql">完整的sql语句</param>
        /// <param name="parameters">参数</param>
        /// <returns>泛型集合</returns>
        public TEntity QuerySqlQuerySingle(string strSql, SugarParameter[] parameters = null)
        {
            return _db.Ado.SqlQuerySingle<TEntity>(strSql, parameters);
        }

        /// <summary>
        /// 根据sql语句查询
        /// </summary>
        /// <param name="strSql">完整的sql语句</param>
        /// <param name="parameters">参数</param>
        /// <returns>泛型集合</returns>
        public async Task<TEntity> QuerySqlQuerySingleAsync(string strSql, SugarParameter[] parameters = null)
        {
            return await _db.Ado.SqlQuerySingleAsync<TEntity>(strSql, parameters);
        }

        /// <summary>
        /// 根据sql语句查询
        /// </summary>
        /// <param name="strSql">完整的sql语句</param>
        /// <param name="parameters">参数</param>
        /// <returns>泛型集合</returns>
        public TResult QuerySqlQuerySingle<TResult>(string strSql, SugarParameter[] parameters = null)
        {
            return _db.Ado.SqlQuerySingle<TResult>(strSql, parameters);
        }

        /// <summary>
        /// 根据sql语句查询
        /// </summary>
        /// <param name="strSql">完整的sql语句</param>
        /// <param name="parameters">参数</param>
        /// <returns>泛型集合</returns>
        public async Task<TResult> QuerySqlQuerySingleAsync<TResult>(string strSql, SugarParameter[] parameters = null)
        {
            return await _db.Ado.SqlQuerySingleAsync<TResult>(strSql, parameters);
        }

        /// <summary>
        ///查询-2表查询返回结果实体
        /// </summary>
        /// <typeparam name="T">实体1</typeparam>
        /// <typeparam name="T2">实体2</typeparam>
        /// <typeparam name="TResult">返回对象</typeparam>
        /// <param name="joinExpression">关联表达式 (join1,join2) => new object[] {JoinType.Left,join1.UserNo==join2.UserNo}</param>
        /// <param name="selectExpression">返回表达式 (s1, s2) => new { Id =s1.UserNo, Id1 = s2.UserNo}</param>
        /// <param name="whereLambda">查询表达式 (w1, w2) =>w1.UserNo == "")</param>
        /// <returns>值</returns>
        public TResult QueryMuchEntity<T, T2, TResult>(Expression<Func<T, T2, object[]>> joinExpression, Expression<Func<T, T2, TResult>> selectExpression, Expression<Func<T, T2, bool>> whereLambda = null, string strOrderByFileds = null) where T : class, new()
        {
            if (whereLambda == null)
            {
                return _db.Queryable(joinExpression).OrderByIF(strOrderByFileds != null, strOrderByFileds).Select(selectExpression).First();
            }
            return _db.Queryable(joinExpression).Where(whereLambda).OrderByIF(strOrderByFileds != null, strOrderByFileds).Select(selectExpression).First();
        }

        /// <summary>
        ///查询-2表查询返回结果实体
        /// </summary>
        /// <typeparam name="T">实体1</typeparam>
        /// <typeparam name="T2">实体2</typeparam>
        /// <typeparam name="TResult">返回对象</typeparam>
        /// <param name="joinExpression">关联表达式 (join1,join2) => new object[] {JoinType.Left,join1.UserNo==join2.UserNo}</param>
        /// <param name="selectExpression">返回表达式 (s1, s2) => new { Id =s1.UserNo, Id1 = s2.UserNo}</param>
        /// <param name="whereLambda">查询表达式 (w1, w2) =>w1.UserNo == "")</param>
        /// <returns>值</returns>
        public async Task<TResult> QueryMuchEntityAsync<T, T2, TResult>(Expression<Func<T, T2, object[]>> joinExpression, Expression<Func<T, T2, TResult>> selectExpression, Expression<Func<T, T2, bool>> whereLambda = null) where T : class, new()
        {
            if (whereLambda == null)
            {
                return await _db.Queryable(joinExpression).Select(selectExpression).FirstAsync();
            }
            return await _db.Queryable(joinExpression).Where(whereLambda).Select(selectExpression).FirstAsync();
        }

        ///  <summary>
        /// 查询-2表查询返回结果实体
        ///  </summary>
        ///  <typeparam name="T">实体1</typeparam>
        ///  <typeparam name="T2">实体2</typeparam>
        ///  <typeparam name="TResult">返回对象</typeparam>
        ///  <param name="tableName">表名称</param>
        ///  <param name="joinExpression">关联表达式 (join1,join2) => new object[] {JoinType.Left,join1.UserNo==join2.UserNo}</param>
        ///  <param name="selectExpression">返回表达式 (s1, s2) => new { Id =s1.UserNo, Id1 = s2.UserNo}</param>
        ///  <param name="whereLambda">查询表达式 (w1, w2) =>w1.UserNo == "")</param>
        ///  <returns>值</returns>
        public TResult QueryMuchEntity<T, T2, TResult>(string tableName, Expression<Func<T, T2, object[]>> joinExpression, Expression<Func<T, T2, TResult>> selectExpression, Expression<Func<T, T2, bool>> whereLambda = null) where T : class, new()
        {
            if (whereLambda == null)
            {
                return _db.Queryable(joinExpression).AS(tableName).Select(selectExpression).First();
            }
            return _db.Queryable(joinExpression).AS(tableName).Where(whereLambda).Select(selectExpression).First();
        }

        ///  <summary>
        /// 查询-2表查询返回结果实体
        ///  </summary>
        ///  <typeparam name="T">实体1</typeparam>
        ///  <typeparam name="T2">实体2</typeparam>
        ///  <typeparam name="TResult">返回对象</typeparam>
        ///  <param name="tableName">表名称</param>
        ///  <param name="joinExpression">关联表达式 (join1,join2) => new object[] {JoinType.Left,join1.UserNo==join2.UserNo}</param>
        ///  <param name="selectExpression">返回表达式 (s1, s2) => new { Id =s1.UserNo, Id1 = s2.UserNo}</param>
        ///  <param name="whereLambda">查询表达式 (w1, w2) =>w1.UserNo == "")</param>
        ///  <returns>值</returns>
        public async Task<TResult> QueryMuchEntityAsync<T, T2, TResult>(string tableName, Expression<Func<T, T2, object[]>> joinExpression, Expression<Func<T, T2, TResult>> selectExpression, Expression<Func<T, T2, bool>> whereLambda = null) where T : class, new()
        {
            if (whereLambda == null)
            {
                return await _db.Queryable(joinExpression).AS(tableName).Select(selectExpression).FirstAsync();
            }
            return await _db.Queryable(joinExpression).AS(tableName).Where(whereLambda).Select(selectExpression).FirstAsync();
        }

        ///// <summary>
        /////查询-4表查询返回结果实体
        ///// </summary>
        ///// <typeparam name="T">实体1</typeparam>
        ///// <typeparam name="T2">实体2</typeparam>
        ///// <typeparam name="T3">实体3</typeparam>
        ///// <typeparam name="T4">实体4</typeparam>
        ///// <typeparam name="TResult">返回对象</typeparam>
        ///// <param name="joinExpression">关联表达式 (join1,join2) => new object[] {JoinType.Left,join1.UserNo==join2.UserNo}</param>
        ///// <param name="selectExpression">返回表达式 (s1, s2) => new { Id =s1.UserNo, Id1 = s2.UserNo}</param>
        ///// <param name="whereLambda">查询表达式 (w1, w2) =>w1.UserNo == "")</param>
        ///// <returns>值</returns>
        //public TResult QueryMuchEntity<T, T2, T3, T4, TResult>(Expression<Func<T, T2, T3, T4, object[]>> joinExpression, Expression<Func<T, T2, T3, T4, TResult>> selectExpression, Expression<Func<T, T2, T3, T4, bool>> whereLambda = null, string strOrderByFileds = null) where T : class, new()
        //{
        //    if (whereLambda == null)
        //    {
        //        return _db.Queryable(joinExpression).OrderByIF(strOrderByFileds != null, strOrderByFileds).Select(selectExpression).First();
        //    }
        //    return _db.Queryable(joinExpression).Where(whereLambda).OrderByIF(strOrderByFileds != null, strOrderByFileds).Select(selectExpression).First();
        //}

        #endregion 获取单个实体

        #region 查询列表

        /// <summary>
        /// 查询所有数据
        /// </summary>
        /// <returns>数据列表</returns>
        public List<TEntity> Query()
        {
            return _db.Queryable<TEntity>().ToList();
        }

        /// <summary>
        /// 查询所有数据
        /// </summary>
        /// <returns>数据列表</returns>
        public async Task<List<TEntity>> QueryAsync()
        {
            return await _db.Queryable<TEntity>().ToListAsync();
        }

        /// <summary>
        /// 查询数据列表
        /// </summary>
        /// <param name="strWhere">条件</param>
        /// <returns>数据列表</returns>
        public List<TEntity> Query(string strWhere)
        {
            return _db.Queryable<TEntity>().WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).ToList();
        }

        /// <summary>
        /// 查询数据列表
        /// </summary>
        /// <param name="strWhere">条件</param>
        /// <returns>数据列表</returns>
        public async Task<List<TEntity>> QueryAsync(string strWhere)
        {
            return await _db.Queryable<TEntity>().WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).ToListAsync();
        }

        /// <summary>
        /// 查询数据列表
        /// </summary>
        /// <param name="strWhere">条件</param>
        /// <returns>数据列表</returns>
        public List<TResult> Query<TResult>(string strWhere)
        {
            return _db.Queryable<TEntity>().WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).Select<TResult>().ToList();
        }

        /// <summary>
        /// 查询数据列表
        /// </summary>
        /// <param name="strWhere">条件</param>
        /// <returns>数据列表</returns>
        public async Task<List<TResult>> QueryAsync<TResult>(string strWhere)
        {
            return await _db.Queryable<TEntity>().WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).Select<TResult>().ToListAsync();
        }

        /// <summary>
        /// 查询数据列表
        /// </summary>
        /// <param name="whereExpression">whereExpression</param>
        /// <returns>数据列表</returns>
        public List<TEntity> Query(Expression<Func<TEntity, bool>> whereExpression)
        {
            return _db.Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).ToList();
        }

        /// <summary>
        /// 查询数据列表
        /// </summary>
        /// <param name="whereExpression">whereExpression</param>
        /// <returns>数据列表</returns>
        public async Task<List<TEntity>> QueryAsync(Expression<Func<TEntity, bool>> whereExpression)
        {
            return await _db.Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).ToListAsync();
        }

        /// <summary>
        /// 按照特定列查询数据列表
        /// </summary>
        /// <param name="whereExpression">whereExpression</param>
        /// <returns>数据列表</returns>
        public List<TResult> Query<TResult>(Expression<Func<TEntity, bool>> whereExpression)
        {
            return _db.Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).Select<TResult>().ToList();
        }

        /// <summary>
        /// 按照特定列查询数据列表
        /// </summary>
        /// <param name="whereExpression">whereExpression</param>
        /// <returns>数据列表</returns>
        public async Task<List<TResult>> QueryAsync<TResult>(Expression<Func<TEntity, bool>> whereExpression)
        {
            return await _db.Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).Select<TResult>().ToListAsync();
        }

        /// <summary>
        /// 查询数据列表
        /// </summary>
        /// <param name="tableName">表名称</param>
        /// <param name="whereExpression">whereExpression</param>
        /// <returns>数据列表</returns>
        public List<TEntity> Query(string tableName, Expression<Func<TEntity, bool>> whereExpression)
        {
            return _db.Queryable<TEntity>().AS(tableName).WhereIF(whereExpression != null, whereExpression).ToList();
        }

        /// <summary>
        /// 查询数据列表
        /// </summary>
        /// <param name="tableName">表名称</param>
        /// <param name="whereExpression">whereExpression</param>
        /// <returns>数据列表</returns>
        public async Task<List<TEntity>> QueryAsync(string tableName, Expression<Func<TEntity, bool>> whereExpression)
        {
            return await _db.Queryable<TEntity>().AS(tableName).WhereIF(whereExpression != null, whereExpression).ToListAsync();
        }

        /// <summary>
        /// 按照特定列查询数据列表
        /// </summary>
        /// <param name="tableName">表名称</param>
        /// <param name="whereExpression">whereExpression</param>
        /// <returns>数据列表</returns>
        public List<TResult> Query<TResult>(string tableName, Expression<Func<TEntity, bool>> whereExpression)
        {
            return _db.Queryable<TEntity>().AS(tableName).WhereIF(whereExpression != null, whereExpression).Select<TResult>().ToList();
        }

        /// <summary>
        /// 按照特定列查询数据列表
        /// </summary>
        /// <param name="tableName">表名称</param>
        /// <param name="whereExpression">whereExpression</param>
        /// <returns>数据列表</returns>
        public async Task<List<TResult>> QueryAsync<TResult>(string tableName, Expression<Func<TEntity, bool>> whereExpression)
        {
            return await _db.Queryable<TEntity>().AS(tableName).WhereIF(whereExpression != null, whereExpression).Select<TResult>().ToListAsync();
        }

        /// <summary>
        /// 按照特定列查询数据列表带条件排序
        /// </summary>
        /// <param name="whereExpression">whereExpression</param>
        /// <param name="strOrderByFileds">排序条件</param>
        /// <returns>数据列表</returns>
        public List<TResult> Query<TResult>(Expression<Func<TEntity, bool>> whereExpression, string strOrderByFileds)
        {
            return _db.Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).OrderByIF(strOrderByFileds != null, strOrderByFileds).Select<TResult>().ToList();
        }

        /// <summary>
        /// 按照特定列查询数据列表带条件排序
        /// </summary>
        /// <param name="tableName">表名称</param>
        /// <param name="whereExpression">whereExpression</param>
        /// <param name="strOrderByFileds">排序条件</param>
        /// <returns>数据列表</returns>
        public async Task<List<TResult>> QueryAsync<TResult>(Expression<Func<TEntity, bool>> whereExpression, string strOrderByFileds)
        {
            return await _db.Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).OrderByIF(strOrderByFileds != null, strOrderByFileds).Select<TResult>().ToListAsync();
        }

        /// <summary>
        /// 按照特定列查询数据列表带条件排序
        /// </summary>
        /// <param name="tableName">表名称</param>
        /// <param name="whereExpression">whereExpression</param>
        /// <param name="strOrderByFileds">排序条件</param>
        /// <returns>数据列表</returns>
        public List<TResult> Query<TResult>(string tableName, Expression<Func<TEntity, bool>> whereExpression, string strOrderByFileds)
        {
            return _db.Queryable<TEntity>().AS(tableName).WhereIF(whereExpression != null, whereExpression).OrderByIF(strOrderByFileds != null, strOrderByFileds).Select<TResult>().ToList();
        }

        /// <summary>
        /// 按照特定列查询数据列表带条件排序
        /// </summary>
        /// <param name="whereExpression">whereExpression</param>
        /// <param name="strOrderByFileds">排序条件</param>
        /// <returns>数据列表</returns>
        public async Task<List<TResult>> QueryAsync<TResult>(string tableName, Expression<Func<TEntity, bool>> whereExpression, string strOrderByFileds)
        {
            return await _db.Queryable<TEntity>().AS(tableName).WhereIF(whereExpression != null, whereExpression).OrderByIF(strOrderByFileds != null, strOrderByFileds).Select<TResult>().ToListAsync();
        }

        /// <summary>
        /// 按照特定列查询数据列表带条件排序
        /// </summary>
        /// <param name="tableName">表名称</param>
        /// <typeparam name="TResult">返回指定结构数据实体</typeparam>
        /// <param name="expression">查询实体条件</param>
        /// <param name="strOrderByFileds">排序条件</param>
        /// <returns></returns>
        public List<TResult> Query<TResult>(Expression<Func<TEntity, TResult>> expression, string strOrderByFileds)
        {
            return _db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).Select(expression).ToList();
        }

        /// <summary>
        /// 按照特定列查询数据列表带条件排序
        /// </summary>
        /// <typeparam name="TResult">返回指定结构数据实体</typeparam>
        /// <param name="expression">查询实体条件</param>
        /// <param name="strOrderByFileds">排序条件</param>
        /// <returns></returns>
        public async Task<List<TResult>> QueryAsync<TResult>(Expression<Func<TEntity, TResult>> expression, string strOrderByFileds)
        {
            return await _db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).Select(expression).ToListAsync();
        }

        /// <summary>
        /// 按照特定列查询数据列表带条件排序
        /// </summary>
        /// <typeparam name="TResult">返回指定结构数据实体</typeparam>
        /// <param name="tableName">表名称</param>
        /// <param name="expression">查询实体条件</param>
        /// <param name="strOrderByFileds">排序条件</param>
        /// <returns></returns>
        public List<TResult> Query<TResult>(string tableName, Expression<Func<TEntity, TResult>> expression, string strOrderByFileds)
        {
            return _db.Queryable<TEntity>().AS(tableName).OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).Select(expression).ToList();
        }

        /// <summary>
        /// 按照特定列查询数据列表带条件排序
        /// </summary>
        /// <typeparam name="TResult">返回指定结构数据实体</typeparam>
        /// <param name="tableName">表名称</param>
        /// <param name="expression">查询实体条件</param>
        /// <param name="strOrderByFileds">排序条件</param>
        /// <returns></returns>
        public async Task<List<TResult>> QueryAsync<TResult>(string tableName, Expression<Func<TEntity, TResult>> expression, string strOrderByFileds)
        {
            return await _db.Queryable<TEntity>().AS(tableName).OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).Select(expression).ToListAsync();
        }

        /// <summary>
        /// 按照特定列查询数据列表带条件排序
        /// </summary>
        /// <typeparam name="TResult">返回指定结构数据实体</typeparam>
        /// <param name="whereExpression">过滤条件</param>
        /// <param name="expression">查询实体条件</param>
        /// <param name="strOrderByFileds">排序条件</param>
        /// <returns></returns>
        public List<TResult> Query<TResult>(Expression<Func<TEntity, TResult>> expression, Expression<Func<TEntity, bool>> whereExpression, string strOrderByFileds)
        {
            return _db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression).Select(expression).ToList();
        }

        /// <summary>
        /// 按照特定列查询数据列表带条件排序
        /// </summary>
        /// <typeparam name="TResult">返回指定结构数据实体</typeparam>
        /// <param name="whereExpression">过滤条件</param>
        /// <param name="expression">查询实体条件</param>
        /// <param name="strOrderByFileds">排序条件</param>
        /// <returns></returns>
        public async Task<List<TResult>> QueryAsync<TResult>(Expression<Func<TEntity, TResult>> expression, Expression<Func<TEntity, bool>> whereExpression, string strOrderByFileds)
        {
            return await _db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression).Select(expression).ToListAsync();
        }

        /// <summary>
        /// 按照特定列查询数据列表带条件排序
        /// </summary>
        /// <param name="tableName">表名称</param>
        /// <typeparam name="TResult">返回指定结构数据实体</typeparam>
        /// <param name="whereExpression">过滤条件</param>
        /// <param name="expression">查询实体条件</param>
        /// <param name="strOrderByFileds">排序条件</param>
        /// <returns></returns>
        public List<TResult> Query<TResult>(string tableName, Expression<Func<TEntity, TResult>> expression, Expression<Func<TEntity, bool>> whereExpression, string strOrderByFileds)
        {
            return _db.Queryable<TEntity>().AS(tableName).OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression).Select(expression).ToList();
        }

        /// <summary>
        /// 按照特定列查询数据列表带条件排序
        /// </summary>
        /// <param name="tableName">表名称</param>
        /// <typeparam name="TResult">返回指定结构数据实体</typeparam>
        /// <param name="whereExpression">过滤条件</param>
        /// <param name="expression">查询实体条件</param>
        /// <param name="strOrderByFileds">排序条件</param>
        /// <returns></returns>
        public async Task<List<TResult>> QueryAsync<TResult>(string tableName, Expression<Func<TEntity, TResult>> expression, Expression<Func<TEntity, bool>> whereExpression, string strOrderByFileds)
        {
            return await _db.Queryable<TEntity>().AS(tableName).OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression).Select(expression).ToListAsync();
        }

        /// <summary>
        /// 查询一个列表
        /// </summary>
        /// <param name="whereExpression">条件表达式</param>
        /// <param name="strOrderByFileds">排序字段，如name asc,age desc</param>
        /// <returns>数据列表</returns>
        public List<TEntity> Query(Expression<Func<TEntity, bool>> whereExpression, string strOrderByFileds)
        {
            return _db.Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).OrderByIF(strOrderByFileds != null, strOrderByFileds).ToList();
        }

        /// <summary>
        /// 查询一个列表
        /// </summary>
        /// <param name="whereExpression">条件表达式</param>
        /// <param name="strOrderByFileds">排序字段，如name asc,age desc</param>
        /// <returns>数据列表</returns>
        public async Task<List<TEntity>> QueryAsync(Expression<Func<TEntity, bool>> whereExpression, string strOrderByFileds)
        {
            return await _db.Queryable<TEntity>().WhereIF(whereExpression != null, whereExpression).OrderByIF(strOrderByFileds != null, strOrderByFileds).ToListAsync();
        }

        /// <summary>
        /// 查询一个列表
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <param name="orderByExpression"></param>
        /// <param name="isAsc"></param>
        /// <returns></returns>
        public List<TEntity> Query(Expression<Func<TEntity, bool>> whereExpression, Expression<Func<TEntity, object>> orderByExpression, bool isAsc = true)
        {
            return _db.Queryable<TEntity>().OrderByIF(orderByExpression != null, orderByExpression, isAsc ? OrderByType.Asc : OrderByType.Desc).WhereIF(whereExpression != null, whereExpression).ToList();
        }

        /// <summary>
        /// 查询一个列表
        /// </summary>
        /// <param name="whereExpression"></param>
        /// <param name="orderByExpression"></param>
        /// <param name="isAsc"></param>
        /// <returns></returns>
        public async Task<List<TEntity>> QueryAsync(Expression<Func<TEntity, bool>> whereExpression, Expression<Func<TEntity, object>> orderByExpression, bool isAsc = true)
        {
            return await _db.Queryable<TEntity>().OrderByIF(orderByExpression != null, orderByExpression, isAsc ? OrderByType.Asc : OrderByType.Desc).WhereIF(whereExpression != null, whereExpression).ToListAsync();
        }

        /// <summary>
        /// 查询一个列表
        /// </summary>
        /// <param name="tableName">表名称</param>
        /// <param name="whereExpression">条件表达式</param>
        /// <param name="strOrderByFileds">排序字段，如name asc,age desc</param>
        /// <returns>数据列表</returns>
        public List<TEntity> Query(string tableName, Expression<Func<TEntity, bool>> whereExpression, string strOrderByFileds)
        {
            return _db.Queryable<TEntity>().AS(tableName).WhereIF(whereExpression != null, whereExpression).OrderByIF(strOrderByFileds != null, strOrderByFileds).ToList();
        }

        /// <summary>
        /// 查询一个列表
        /// </summary>
        /// <param name="tableName">表名称</param>
        /// <param name="whereExpression">条件表达式</param>
        /// <param name="strOrderByFileds">排序字段，如name asc,age desc</param>
        /// <returns>数据列表</returns>
        public async Task<List<TEntity>> QueryAsync(string tableName, Expression<Func<TEntity, bool>> whereExpression, string strOrderByFileds)
        {
            return await _db.Queryable<TEntity>().AS(tableName).WhereIF(whereExpression != null, whereExpression).OrderByIF(strOrderByFileds != null, strOrderByFileds).ToListAsync();
        }

        /// <summary>
        /// 查询一个列表
        /// </summary>
        /// <param name="tableName">表名称</param>
        /// <param name="whereExpression"></param>
        /// <param name="orderByExpression"></param>
        /// <param name="isAsc"></param>
        /// <returns></returns>
        public List<TEntity> Query(string tableName, Expression<Func<TEntity, bool>> whereExpression, Expression<Func<TEntity, object>> orderByExpression, bool isAsc = true)
        {
            return _db.Queryable<TEntity>().AS(tableName).OrderByIF(orderByExpression != null, orderByExpression, isAsc ? OrderByType.Asc : OrderByType.Desc).WhereIF(whereExpression != null, whereExpression).ToList();
        }

        /// <summary>
        /// 查询一个列表
        /// </summary>
        /// <param name="tableName">表名称</param>
        /// <param name="whereExpression">条件表达式</param>
        /// <param name="orderByExpression">排序字段</param>
        /// <param name="isAsc"></param>
        /// <returns></returns>
        public async Task<List<TEntity>> QueryAsync(string tableName, Expression<Func<TEntity, bool>> whereExpression, Expression<Func<TEntity, object>> orderByExpression, bool isAsc = true)
        {
            return await _db.Queryable<TEntity>().AS(tableName).OrderByIF(orderByExpression != null, orderByExpression, isAsc ? OrderByType.Asc : OrderByType.Desc).WhereIF(whereExpression != null, whereExpression).ToListAsync();
        }

        /// <summary>
        /// 查询一个列表
        /// </summary>
        /// <param name="strWhere">条件</param>
        /// <param name="strOrderByFileds">排序字段，如name asc,age desc</param>
        /// <returns>数据列表</returns>
        public List<TEntity> Query(string strWhere, string strOrderByFileds)
        {
            return _db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).ToList();
        }

        /// <summary>
        /// 查询一个列表
        /// </summary>
        /// <param name="strWhere">条件</param>
        /// <param name="strOrderByFileds">排序字段，如name asc,age desc</param>
        /// <returns>数据列表</returns>
        public async Task<List<TEntity>> QueryAsync(string strWhere, string strOrderByFileds)
        {
            return await _db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).ToListAsync();
        }

        /// <summary>
        /// 根据sql语句查询
        /// </summary>
        /// <param name="strSql">完整的sql语句</param>
        /// <param name="parameters">参数</param>
        /// <returns>泛型集合</returns>
        public List<TEntity> QuerySql(string strSql, SugarParameter[] parameters = null)
        {
            return _db.Ado.SqlQuery<TEntity>(strSql, parameters);
        }

        /// <summary>
        /// 根据sql语句查询
        /// </summary>
        /// <param name="strSql">完整的sql语句</param>
        /// <param name="parameters">参数</param>
        /// <returns>泛型集合</returns>
        public async Task<List<TEntity>> QuerySqlAsync(string strSql, SugarParameter[] parameters = null)
        {
            return await _db.Ado.SqlQueryAsync<TEntity>(strSql, parameters);
        }

        /// <summary>
        /// 根据sql语句查询
        /// </summary>
        /// <param name="strSql">完整的sql语句</param>
        /// <param name="parameters">参数</param>
        /// <returns>DataTable</returns>
        public DataTable QueryTable(string strSql, SugarParameter[] parameters = null)
        {
            return _db.Ado.GetDataTable(strSql, parameters);
        }

        /// <summary>
        /// 根据sql语句查询
        /// </summary>
        /// <param name="strSql">完整的sql语句</param>
        /// <param name="parameters">参数</param>
        /// <returns>DataTable</returns>
        public async Task<DataTable> QueryTableAsync(string strSql, SugarParameter[] parameters = null)
        {
            return await _db.Ado.GetDataTableAsync(strSql, parameters);
        }

        /// <summary>
        ///查询-2多表查询返回数据列表
        /// </summary>
        /// <typeparam name="T">实体1</typeparam>
        /// <typeparam name="T2">实体2</typeparam>
        /// <typeparam name="TResult">返回对象</typeparam>
        /// <param name="joinExpression">关联表达式 (join1,join2) => new object[] {JoinType.Left,join1.UserNo==join2.UserNo}</param>
        /// <param name="selectExpression">返回表达式 (s1, s2) => new { Id =s1.UserNo, Id1 = s2.UserNo}</param>
        /// <param name="whereLambda">查询表达式 (w1, w2) =>w1.UserNo == "")</param>
        /// <returns>值</returns>
        public List<TResult> QueryMuch<T, T2, TResult>(Expression<Func<T, T2, object[]>> joinExpression, Expression<Func<T, T2, TResult>> selectExpression, Expression<Func<T, T2, bool>> whereLambda = null) where T : class, new()
        {
            if (whereLambda == null)
            {
                return _db.Queryable(joinExpression).Select(selectExpression).ToList();
            }
            return _db.Queryable(joinExpression).Where(whereLambda).Select(selectExpression).ToList();
        }

        /// <summary>
        ///查询-2多表查询返回数据列表
        /// </summary>
        /// <typeparam name="T">实体1</typeparam>
        /// <typeparam name="T2">实体2</typeparam>
        /// <typeparam name="TResult">返回对象</typeparam>
        /// <param name="joinExpression">关联表达式 (join1,join2) => new object[] {JoinType.Left,join1.UserNo==join2.UserNo}</param>
        /// <param name="selectExpression">返回表达式 (s1, s2) => new { Id =s1.UserNo, Id1 = s2.UserNo}</param>
        /// <param name="whereLambda">查询表达式 (w1, w2) =>w1.UserNo == "")</param>
        /// <returns>值</returns>
        public async Task<List<TResult>> QueryMuchAsync<T, T2, TResult>(Expression<Func<T, T2, object[]>> joinExpression, Expression<Func<T, T2, TResult>> selectExpression, Expression<Func<T, T2, bool>> whereLambda = null) where T : class, new()
        {
            if (whereLambda == null)
            {
                return await _db.Queryable(joinExpression).Select(selectExpression).ToListAsync();
            }
            return await _db.Queryable(joinExpression).Where(whereLambda).Select(selectExpression).ToListAsync();
        }

        /// <summary>
        ///查询-2多表查询返回数据列表
        /// </summary>
        /// <typeparam name="T">实体1</typeparam>
        /// <typeparam name="T2">实体2</typeparam>
        /// <typeparam name="TResult">返回对象</typeparam>
        /// <param name="joinExpression">关联表达式 (join1,join2) => new object[] {JoinType.Left,join1.UserNo==join2.UserNo}</param>
        /// <param name="selectExpression">返回表达式 (s1, s2) => new { Id =s1.UserNo, Id1 = s2.UserNo}</param>
        /// <param name="whereLambda">查询表达式 (w1, w2) =>w1.UserNo == "")</param>
        /// <returns>值</returns>
        public List<TResult> QueryMuch<T, T2, TResult>(Expression<Func<T, T2, object[]>> joinExpression, Expression<Func<T, T2, TResult>> selectExpression, Expression<Func<T, T2, bool>> whereLambda = null, string strOrderByFileds = null) where T : class, new()
        {
            if (whereLambda == null)
            {
                return _db.Queryable(joinExpression).Select(selectExpression).ToList();
            }
            return _db.Queryable(joinExpression).OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).Where(whereLambda).Select(selectExpression).ToList();
        }

        ///  <summary>
        /// 查询-2多表查询返回数据列表
        ///  </summary>
        ///  <typeparam name="T">实体1</typeparam>
        ///  <typeparam name="T2">实体2</typeparam>
        ///  <typeparam name="TResult">返回对象</typeparam>
        ///  <param name="tableName">表名称</param>
        ///  <param name="joinExpression">关联表达式 (join1,join2) => new object[] {JoinType.Left,join1.UserNo==join2.UserNo}</param>
        ///  <param name="selectExpression">返回表达式 (s1, s2) => new { Id =s1.UserNo, Id1 = s2.UserNo}</param>
        ///  <param name="whereLambda">查询表达式 (w1, w2) =>w1.UserNo == "")</param>
        ///  <param name="strOrderByFileds">排序字段</param>
        ///  <returns>值</returns>
        public List<TResult> QueryMuch<T, T2, TResult>(string tableName, Expression<Func<T, T2, object[]>> joinExpression, Expression<Func<T, T2, TResult>> selectExpression, Expression<Func<T, T2, bool>> whereLambda = null, string strOrderByFileds = null) where T : class, new()
        {
            if (whereLambda == null)
            {
                return _db.Queryable(joinExpression).AS(tableName).Select(selectExpression).ToList();
            }
            return _db.Queryable(joinExpression).AS(tableName).OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).Where(whereLambda).Select(selectExpression).ToList();
        }

        ///  <summary>
        /// 查询-2多表查询返回数据列表
        ///  </summary>
        ///  <typeparam name="T">实体1</typeparam>
        ///  <typeparam name="T2">实体2</typeparam>
        ///  <typeparam name="TResult">返回对象</typeparam>
        ///  <param name="tableName">表名称</param>
        ///  <param name="joinExpression">关联表达式 (join1,join2) => new object[] {JoinType.Left,join1.UserNo==join2.UserNo}</param>
        ///  <param name="selectExpression">返回表达式 (s1, s2) => new { Id =s1.UserNo, Id1 = s2.UserNo}</param>
        ///  <param name="whereLambda">查询表达式 (w1, w2) =>w1.UserNo == "")</param>
        ///  <param name="strOrderByFileds">排序字段</param>
        ///  <returns>值</returns>
        public async Task<List<TResult>> QueryMuchAsync<T, T2, TResult>(string tableName, Expression<Func<T, T2, object[]>> joinExpression, Expression<Func<T, T2, TResult>> selectExpression, Expression<Func<T, T2, bool>> whereLambda = null, string strOrderByFileds = null) where T : class, new()
        {
            if (whereLambda == null)
            {
                return await _db.Queryable(joinExpression).AS(tableName).Select(selectExpression).ToListAsync();
            }
            return await _db.Queryable(joinExpression).AS(tableName).OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).Where(whereLambda).Select(selectExpression).ToListAsync();
        }

        /// <summary>
        ///查询-3表查询返回数据列表
        /// </summary>
        /// <typeparam name="T">实体1</typeparam>
        /// <typeparam name="T2">实体2</typeparam>
        /// <typeparam name="T3">实体3</typeparam>
        /// <typeparam name="TResult">返回对象</typeparam>
        /// <param name="joinExpression">关联表达式 (join1,join2) => new object[] {JoinType.Left,join1.UserNo==join2.UserNo}</param>
        /// <param name="selectExpression">返回表达式 (s1, s2) => new { Id =s1.UserNo, Id1 = s2.UserNo}</param>
        /// <param name="whereLambda">查询表达式 (w1, w2) =>w1.UserNo == "")</param>
        /// <returns>值</returns>
        public List<TResult> QueryMuch<T, T2, T3, TResult>(Expression<Func<T, T2, T3, object[]>> joinExpression, Expression<Func<T, T2, T3, TResult>> selectExpression, Expression<Func<T, T2, T3, bool>> whereLambda = null) where T : class, new()
        {
            if (whereLambda == null)
            {
                return _db.Queryable(joinExpression).Select(selectExpression).ToList();
            }
            return _db.Queryable(joinExpression).Where(whereLambda).Select(selectExpression).ToList();
        }

        /// <summary>
        ///查询-3表查询返回数据列表
        /// </summary>
        /// <typeparam name="T">实体1</typeparam>
        /// <typeparam name="T2">实体2</typeparam>
        /// <typeparam name="T3">实体3</typeparam>
        /// <typeparam name="TResult">返回对象</typeparam>
        /// <param name="joinExpression">关联表达式 (join1,join2) => new object[] {JoinType.Left,join1.UserNo==join2.UserNo}</param>
        /// <param name="selectExpression">返回表达式 (s1, s2) => new { Id =s1.UserNo, Id1 = s2.UserNo}</param>
        /// <param name="whereLambda">查询表达式 (w1, w2) =>w1.UserNo == "")</param>
        /// <returns>值</returns>
        public async Task<List<TResult>> QueryMuchAsync<T, T2, T3, TResult>(Expression<Func<T, T2, T3, object[]>> joinExpression, Expression<Func<T, T2, T3, TResult>> selectExpression, Expression<Func<T, T2, T3, bool>> whereLambda = null) where T : class, new()
        {
            if (whereLambda == null)
            {
                return await _db.Queryable(joinExpression).Select(selectExpression).ToListAsync();
            }
            return await _db.Queryable(joinExpression).Where(whereLambda).Select(selectExpression).ToListAsync();
        }

        /// <summary>
        ///查询-4表查询返回数据列表
        /// </summary>
        /// <typeparam name="T">实体1</typeparam>
        /// <typeparam name="T2">实体2</typeparam>
        /// <typeparam name="T3">实体3</typeparam>
        /// <typeparam name="T4">实体3</typeparam>
        /// <typeparam name="TResult">返回对象</typeparam>
        /// <param name="joinExpression">关联表达式 (join1,join2) => new object[] {JoinType.Left,join1.UserNo==join2.UserNo}</param>
        /// <param name="selectExpression">返回表达式 (s1, s2) => new { Id =s1.UserNo, Id1 = s2.UserNo}</param>
        /// <param name="whereLambda">查询表达式 (w1, w2) =>w1.UserNo == "")</param>
        /// <returns>值</returns>
        public List<TResult> QueryMuch<T, T2, T3, T4, TResult>(Expression<Func<T, T2, T3, T4, object[]>> joinExpression, Expression<Func<T, T2, T3, T4, TResult>> selectExpression, Expression<Func<T, T2, T3, T4, bool>> whereLambda = null, string strOrderByFileds = null) where T : class, new()
        {
            if (whereLambda == null)
            {
                return _db.Queryable(joinExpression).OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).Select(selectExpression).ToList();
            }
            return _db.Queryable(joinExpression).OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).Where(whereLambda).Select(selectExpression).ToList();
        }

        #endregion 查询列表

        #region 查询前N条数据

        /// <summary>
        /// 功能描述:查询前N条数据
        /// </summary>
        /// <param name="whereExpression">条件表达式</param>
        /// <param name="intTop">前N条</param>
        /// <param name="strOrderByFileds">排序字段，如name asc,age desc</param>
        /// <returns>数据列表</returns>
        public List<TEntity> Query(Expression<Func<TEntity, bool>> whereExpression, int intTop, string strOrderByFileds)
        {
            return _db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression).Take(intTop).ToList();
        }

        /// <summary>
        /// 功能描述:查询前N条数据
        /// </summary>
        /// <param name="whereExpression">条件表达式</param>
        /// <param name="intTop">前N条</param>
        /// <param name="strOrderByFileds">排序字段，如name asc,age desc</param>
        /// <returns>数据列表</returns>
        public async Task<List<TEntity>> QueryAsync(Expression<Func<TEntity, bool>> whereExpression, int intTop, string strOrderByFileds)
        {
            return await _db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression).Take(intTop).ToListAsync();
        }

        /// <summary>
        /// 功能描述:查询前N条数据
        /// </summary>
        /// <param name="tableName">表名称</param>
        /// <param name="whereExpression">条件表达式</param>
        /// <param name="intTop">前N条</param>
        /// <param name="strOrderByFileds">排序字段，如name asc,age desc</param>
        /// <returns>数据列表</returns>
        public List<TEntity> Query(string tableName, Expression<Func<TEntity, bool>> whereExpression, int intTop, string strOrderByFileds)
        {
            return _db.Queryable<TEntity>().AS(tableName).OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression).Take(intTop).ToList();
        }

        /// <summary>
        /// 功能描述:查询前N条数据
        /// </summary>
        /// <param name="tableName">表名称</param>
        /// <param name="whereExpression">条件表达式</param>
        /// <param name="intTop">前N条</param>
        /// <param name="strOrderByFileds">排序字段，如name asc,age desc</param>
        /// <returns>数据列表</returns>
        public async Task<List<TEntity>> QueryAsync(string tableName, Expression<Func<TEntity, bool>> whereExpression, int intTop, string strOrderByFileds)
        {
            return await _db.Queryable<TEntity>().AS(tableName).OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression).Take(intTop).ToListAsync();
        }

        /// <summary>
        /// 功能描述:查询前N条数据
        /// </summary>
        /// <param name="strWhere">条件</param>
        /// <param name="intTop">前N条</param>
        /// <param name="strOrderByFileds">排序字段，如name asc,age desc</param>
        /// <returns>数据列表</returns>
        public List<TEntity> Query(string strWhere, int intTop, string strOrderByFileds)
        {
            return _db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).Take(intTop).ToList();
        }

        /// <summary>
        /// 功能描述:查询前N条数据
        /// </summary>
        /// <param name="strWhere">条件</param>
        /// <param name="intTop">前N条</param>
        /// <param name="strOrderByFileds">排序字段，如name asc,age desc</param>
        /// <returns>数据列表</returns>
        public async Task<List<TEntity>> QueryAsync(string strWhere, int intTop, string strOrderByFileds)
        {
            return await _db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).Take(intTop).ToListAsync();
        }

        #endregion 查询前N条数据

        #region SQL查询

        public DataTable GetDataTable(string sql, List<SugarParameter> parameters = null)
        {
            return Db.Ado.GetDataTable(sql, parameters);
        }

        public DataSet GetDataSet(string sql, List<SugarParameter> parameters = null)
        {
            return Db.Ado.GetDataSetAll(sql, parameters);
        }

        public IDataReader GetDataReader(string sql, List<SugarParameter> parameters = null)
        {
            return Db.Ado.GetDataReader(sql, parameters);
        }

        public int ExecuteQuery(string sql, List<SugarParameter> parameters = null)
        {
            return Db.Ado.ExecuteCommand(sql, parameters);
        }

        public DateTime GetDateTime(string sql, List<SugarParameter> parameters = null)
        {
            return Db.Ado.GetDateTime(sql, parameters);
        }

        public int GetInt(string sql, List<SugarParameter> parameters = null)
        {
            return Db.Ado.GetInt(sql, parameters);
        }

        public string GetString(string sql, List<SugarParameter> parameters = null)
        {
            return Db.Ado.GetString(sql, parameters);
        }

        #endregion SQL查询

        #region 单表分页查询(未返回数据总行数及总页数)

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="whereExpression">条件表达式</param>
        /// <param name="intPageIndex">页码（下标0）</param>
        /// <param name="intPageSize">页大小</param>
        /// <param name="strOrderByFileds">排序字段，如name asc,age desc</param>
        /// <returns>数据列表</returns>
        public List<TEntity> Query(Expression<Func<TEntity, bool>> whereExpression, int intPageIndex, int intPageSize, string strOrderByFileds)
        {
            return _db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression).ToPageList(intPageIndex, intPageSize);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="whereExpression">条件表达式</param>
        /// <param name="intPageIndex">页码（下标0）</param>
        /// <param name="intPageSize">页大小</param>
        /// <param name="strOrderByFileds">排序字段，如name asc,age desc</param>
        /// <returns>数据列表</returns>
        public async Task<List<TEntity>> QueryAsync(Expression<Func<TEntity, bool>> whereExpression, int intPageIndex, int intPageSize, string strOrderByFileds)
        {
            return await _db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression).ToPageListAsync(intPageIndex, intPageSize);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="tableName">表名称</param>
        /// <param name="whereExpression">条件表达式</param>
        /// <param name="intPageIndex">页码（下标0）</param>
        /// <param name="intPageSize">页大小</param>
        /// <param name="strOrderByFileds">排序字段，如name asc,age desc</param>
        /// <returns>数据列表</returns>
        public List<TEntity> Query(string tableName, Expression<Func<TEntity, bool>> whereExpression, int intPageIndex, int intPageSize, string strOrderByFileds)
        {
            return _db.Queryable<TEntity>().AS(tableName).OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression).ToPageList(intPageIndex, intPageSize);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="tableName">表名称</param>
        /// <param name="whereExpression">条件表达式</param>
        /// <param name="intPageIndex">页码（下标0）</param>
        /// <param name="intPageSize">页大小</param>
        /// <param name="strOrderByFileds">排序字段，如name asc,age desc</param>
        /// <returns>数据列表</returns>
        public async Task<List<TEntity>> QueryAsync(string tableName, Expression<Func<TEntity, bool>> whereExpression, int intPageIndex, int intPageSize, string strOrderByFileds)
        {
            return await _db.Queryable<TEntity>().AS(tableName).OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(whereExpression != null, whereExpression).ToPageListAsync(intPageIndex, intPageSize);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="strWhere">条件</param>
        /// <param name="intPageIndex">页码（下标0）</param>
        /// <param name="intPageSize">页大小</param>
        /// <param name="strOrderByFileds">排序字段，如name asc,age desc</param>
        /// <returns>数据列表</returns>
        public List<TEntity> Query(string strWhere, int intPageIndex, int intPageSize, string strOrderByFileds)
        {
            return _db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).ToPageList(intPageIndex, intPageSize);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="strWhere">条件</param>
        /// <param name="intPageIndex">页码（下标0）</param>
        /// <param name="intPageSize">页大小</param>
        /// <param name="strOrderByFileds">排序字段，如name asc,age desc</param>
        /// <returns>数据列表</returns>
        public async Task<List<TEntity>> QueryAsync(string strWhere, int intPageIndex, int intPageSize, string strOrderByFileds)
        {
            return await _db.Queryable<TEntity>().OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).ToPageListAsync(intPageIndex, intPageSize);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="tableName">表名称</param>
        /// <param name="strWhere">条件</param>
        /// <param name="intPageIndex">页码（下标0）</param>
        /// <param name="intPageSize">页大小</param>
        /// <param name="strOrderByFileds">排序字段，如name asc,age desc</param>
        /// <returns>数据列表</returns>
        public List<TEntity> Query(string tableName, string strWhere, int intPageIndex, int intPageSize, string strOrderByFileds)
        {
            return _db.Queryable<TEntity>().AS(tableName).OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).ToPageList(intPageIndex, intPageSize);
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="tableName">表名称</param>
        /// <param name="strWhere">条件</param>
        /// <param name="intPageIndex">页码（下标0）</param>
        /// <param name="intPageSize">页大小</param>
        /// <param name="strOrderByFileds">排序字段，如name asc,age desc</param>
        /// <returns>数据列表</returns>
        public async Task<List<TEntity>> QueryAsync(string tableName, string strWhere, int intPageIndex, int intPageSize, string strOrderByFileds)
        {
            return await _db.Queryable<TEntity>().AS(tableName).OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).WhereIF(!string.IsNullOrEmpty(strWhere), strWhere).ToPageListAsync(intPageIndex, intPageSize);
        }

        #endregion 单表分页查询(未返回数据总行数及总页数)

        #region 单表分页查询(有返回数据总行数及总页数)

        /// <summary>
        /// 分页查询[使用版本]
        /// </summary>
        /// <param name="whereExpression">条件表达式</param>
        /// <param name="intPageIndex">页码（下标0）</param>
        /// <param name="intPageSize">页大小</param>
        /// <param name="strOrderByFileds">排序字段，如name asc,age desc</param>
        /// <returns></returns>
        public async Task<PageModel<TEntity>> QueryPage(Expression<Func<TEntity, bool>> whereExpression, int intPageIndex = 1, int intPageSize = 20, string strOrderByFileds = null)
        {
            RefAsync<int> totalCount = 0;
            var list = await _db.Queryable<TEntity>()
             .OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds)
             .WhereIF(whereExpression != null, whereExpression)
             .ToPageListAsync(intPageIndex, intPageSize, totalCount);

            int pageCount = (Math.Ceiling(totalCount.ObjToDecimal() / intPageSize.ObjToDecimal())).ObjToInt();
            return new PageModel<TEntity>() { dataCount = totalCount, pageCount = pageCount, page = intPageIndex, PageSize = intPageSize, data = list };
        }

        /// <summary>
        /// 分页查询[使用版本]
        /// </summary>
        /// <param name="whereExpression">条件表达式</param>
        /// <param name="intPageIndex">页码（下标0）</param>
        /// <param name="intPageSize">页大小</param>
        /// <param name="strOrderByFileds">排序字段，如name asc,age desc</param>
        /// <returns></returns>
        public async Task<PageModel<TResult>> QueryPage<TResult>(Expression<Func<TEntity, bool>> whereExpression, int intPageIndex = 1, int intPageSize = 20, string strOrderByFileds = null)
        {
            RefAsync<int> totalCount = 0;
            var list = await _db.Queryable<TEntity>()
             .OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds)
             .WhereIF(whereExpression != null, whereExpression)
             .Select<TResult>()
             .ToPageListAsync(intPageIndex, intPageSize, totalCount);

            int pageCount = (Math.Ceiling(totalCount.ObjToDecimal() / intPageSize.ObjToDecimal())).ObjToInt();
            return new PageModel<TResult>() { dataCount = totalCount, pageCount = pageCount, page = intPageIndex, PageSize = intPageSize, data = list };
        }

        /// <summary>
        /// 分页查询[使用版本]
        /// </summary>
        /// <param name="tableName">表名称</param>
        /// <param name="whereExpression">条件表达式</param>
        /// <param name="intPageIndex">页码（下标0）</param>
        /// <param name="intPageSize">页大小</param>
        /// <param name="strOrderByFileds">排序字段，如name asc,age desc</param>
        /// <returns></returns>
        public async Task<PageModel<TEntity>> QueryPage(string tableName, Expression<Func<TEntity, bool>> whereExpression, int intPageIndex = 1, int intPageSize = 20, string strOrderByFileds = null)
        {
            RefAsync<int> totalCount = 0;
            var list = await _db.Queryable<TEntity>().AS(tableName)
             .OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds)
             .WhereIF(whereExpression != null, whereExpression)
             .ToPageListAsync(intPageIndex, intPageSize, totalCount);

            int pageCount = (Math.Ceiling(totalCount.ObjToDecimal() / intPageSize.ObjToDecimal())).ObjToInt();
            return new PageModel<TEntity>() { dataCount = totalCount, pageCount = pageCount, page = intPageIndex, PageSize = intPageSize, data = list };
        }

        /// <summary>
        /// 分页查询[使用版本]
        /// </summary>
        /// <typeparam name="TResult">返回结果实体</typeparam>
        /// <param name="tableName">表名称</param>
        /// <param name="whereExpression">条件表达式</param>
        /// <param name="intPageIndex">页码（下标0）</param>
        /// <param name="intPageSize">页大小</param>
        /// <param name="strOrderByFileds">排序字段，如name asc,age desc</param>
        /// <returns></returns>
        public async Task<PageModel<TResult>> QueryPage<TResult>(string tableName, Expression<Func<TEntity, bool>> whereExpression, int intPageIndex = 1, int intPageSize = 20, string strOrderByFileds = null)
        {
            RefAsync<int> totalCount = 0;
            var list = await _db.Queryable<TEntity>().AS(tableName)
             .OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds)
             .WhereIF(whereExpression != null, whereExpression)
             .Select<TResult>()
             .ToPageListAsync(intPageIndex, intPageSize, totalCount);

            int pageCount = (Math.Ceiling(totalCount.ObjToDecimal() / intPageSize.ObjToDecimal())).ObjToInt();
            return new PageModel<TResult>() { dataCount = totalCount, pageCount = pageCount, page = intPageIndex, PageSize = intPageSize, data = list, extend = tableName };
        }

        /// <summary>
        /// 分页查询[使用版本]
        /// </summary>
        /// <typeparam name="ids">id数组</typeparam>
        /// <typeparam name="TResult">返回结果实体</typeparam>
        /// <param name="tableName">表名称</param>
        /// <param name="whereExpression">条件表达式</param>
        /// <param name="intPageIndex">页码（下标0）</param>
        /// <param name="intPageSize">页大小</param>
        /// <param name="strOrderByFileds">排序字段，如name asc,age desc</param>
        /// <returns></returns>
        public async Task<PageModel<TResult>> QueryPage<TResult>(int[] ids, string baseTableName, Expression<Func<TEntity, bool>> whereExpression, int intPageIndex = 1, int intPageSize = 20, string strOrderByFileds = null)
        {
            RefAsync<int> totalCount = 0;
            List<TResult> list;
            if (whereExpression == null)
            {
                var queryables = new List<ISugarQueryable<TEntity>>();
                foreach (var index in ids)
                {
                    queryables.Add(Db.Queryable<TEntity>().AS($"{index}_{baseTableName}"));
                }
                list = await Db.UnionAll<TEntity>(queryables).OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).Select<TResult>().ToPageListAsync(intPageIndex, intPageSize, totalCount);
            }
            else
            {
                var queryables = new List<ISugarQueryable<TEntity>>();
                foreach (var index in ids)
                {
                    queryables.Add(Db.Queryable<TEntity>().AS($"{index}_{baseTableName}").Where(whereExpression));
                }
                list = await Db.UnionAll<TEntity>(queryables).OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).Select<TResult>().ToPageListAsync(intPageIndex, intPageSize, totalCount);
            }
            int pageCount = (Math.Ceiling(totalCount.ObjToDecimal() / intPageSize.ObjToDecimal())).ObjToInt();
            return new PageModel<TResult>() { dataCount = totalCount, pageCount = pageCount, page = intPageIndex, PageSize = intPageSize, data = list };
        }

        #endregion 单表分页查询(有返回数据总行数及总页数)

        #region 多表分页查询

        /// <summary>
        /// 查询-2表查询分页
        /// </summary>
        /// <typeparam name="T">表1</typeparam>
        /// <typeparam name="T2">表2</typeparam>
        /// <typeparam name="TResult">返回对象</typeparam>
        /// <param name="joinExpression">关联表达式 (join1,join2) => new object[] {JoinType.Left,join1.UserNo==join2.UserNo}</param>
        /// <param name="selectExpression">返回表达式 (s1, s2) => new { Id =s1.UserNo, Id1 = s2.UserNo}</param>
        /// <param name="whereLambda">查询表达式 (w1, w2) =>w1.UserNo == "")</param>
        /// <param name="intPageIndex">分页索引</param>
        /// <param name="intPageSize">每页数量</param>
        /// <param name="strOrderByFileds">排序字段</param>
        /// <returns></returns>
        public async Task<PageModel<TResult>> QueryPage<T, T2, TResult>(Expression<Func<T, T2, object[]>> joinExpression,
           Expression<Func<T, T2, TResult>> selectExpression,
           Expression<Func<T, T2, bool>> whereLambda = null, int intPageIndex = 1, int intPageSize = 20, string strOrderByFileds = null)
        {
            RefAsync<int> totalCount = 0;
            List<TResult> list;
            if (whereLambda == null)
            {
                list = await Db.Queryable(joinExpression).OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).Select(selectExpression).ToPageListAsync(intPageIndex, intPageSize, totalCount);
            }
            else
            {
                list = await Db.Queryable(joinExpression).OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).Where(whereLambda).Select(selectExpression).ToPageListAsync(intPageIndex, intPageSize, totalCount);
            }
            int pageCount = (Math.Ceiling(totalCount.ObjToDecimal() / intPageSize.ObjToDecimal())).ObjToInt();
            return new PageModel<TResult>() { dataCount = totalCount, pageCount = pageCount, page = intPageIndex, PageSize = intPageSize, data = list };
        }

        /// <summary>
        /// 查询-2表查询分页
        /// </summary>
        /// <typeparam name="T">表1</typeparam>
        /// <typeparam name="T2">表2</typeparam>
        /// <typeparam name="TResult">返回对象</typeparam>
        /// <param name="tableName">表名称</param>
        /// <param name="joinExpression">关联表达式 (join1,join2) => new object[] {JoinType.Left,join1.UserNo==join2.UserNo}</param>
        /// <param name="selectExpression">返回表达式 (s1, s2) => new { Id =s1.UserNo, Id1 = s2.UserNo}</param>
        /// <param name="whereLambda">查询表达式 (w1, w2) =>w1.UserNo == "")</param>
        /// <param name="intPageIndex">分页索引</param>
        /// <param name="intPageSize">每页数量</param>
        /// <param name="strOrderByFileds">排序字段</param>
        /// <returns></returns>
        public async Task<PageModel<TResult>> QueryPage<T, T2, TResult>(string tableName, Expression<Func<T, T2, object[]>> joinExpression,
           Expression<Func<T, T2, TResult>> selectExpression,
           Expression<Func<T, T2, bool>> whereLambda = null, int intPageIndex = 1, int intPageSize = 20, string strOrderByFileds = null)
        {
            RefAsync<int> totalCount = 0;
            List<TResult> list;
            if (whereLambda == null)
            {
                list = await Db.Queryable(joinExpression).AS(tableName).OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).Select(selectExpression).ToPageListAsync(intPageIndex, intPageSize, totalCount);
            }
            else
            {
                list = await Db.Queryable(joinExpression).AS(tableName).OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).Where(whereLambda).Select(selectExpression).ToPageListAsync(intPageIndex, intPageSize, totalCount);
            }
            int pageCount = (Math.Ceiling(totalCount.ObjToDecimal() / intPageSize.ObjToDecimal())).ObjToInt();
            return new PageModel<TResult>() { dataCount = totalCount, pageCount = pageCount, page = intPageIndex, PageSize = intPageSize, data = list };
        }

        /// <summary>
        /// 查询-3表查询分页
        /// </summary>
        /// <typeparam name="T">表1</typeparam>
        /// <typeparam name="T2">表2</typeparam>
        /// <typeparam name="T3">表3</typeparam>
        /// <typeparam name="TResult">返回对象</typeparam>
        /// <param name="joinExpression">关联表达式 (join1,join2) => new object[] {JoinType.Left,join1.UserNo==join2.UserNo}</param>
        /// <param name="selectExpression">返回表达式 (s1, s2) => new { Id =s1.UserNo, Id1 = s2.UserNo}</param>
        /// <param name="whereLambda">查询表达式 (w1, w2) =>w1.UserNo == "")</param>
        /// <param name="intPageIndex">分页索引</param>
        /// <param name="intPageSize">每页数量</param>
        /// <param name="strOrderByFileds">排序字段</param>
        /// <returns></returns>
        public async Task<PageModel<TResult>> QueryPage<T, T2, T3, TResult>(Expression<Func<T, T2, T3, object[]>> joinExpression,
           Expression<Func<T, T2, T3, TResult>> selectExpression,
           Expression<Func<T, T2, T3, bool>> whereLambda = null, int intPageIndex = 1, int intPageSize = 20, string strOrderByFileds = null)
        {
            RefAsync<int> totalCount = 0;
            List<TResult> list;
            if (whereLambda == null)
            {
                list = await Db.Queryable(joinExpression).OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).Select(selectExpression).ToPageListAsync(intPageIndex, intPageSize, totalCount);
            }
            else
            {
                list = await Db.Queryable(joinExpression).OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).Where(whereLambda).Select(selectExpression).ToPageListAsync(intPageIndex, intPageSize, totalCount);
            }
            int pageCount = (Math.Ceiling(totalCount.ObjToDecimal() / intPageSize.ObjToDecimal())).ObjToInt();
            return new PageModel<TResult>() { dataCount = totalCount, pageCount = pageCount, page = intPageIndex, PageSize = intPageSize, data = list };
        }

        /// <summary>
        /// 查询-4表查询分页
        /// </summary>
        /// <typeparam name="T">表1</typeparam>
        /// <typeparam name="T2">表2</typeparam>
        /// <typeparam name="T3">表3</typeparam>
        /// <typeparam name="T4">表4</typeparam>
        /// <typeparam name="TResult">返回对象</typeparam>
        /// <param name="joinExpression">关联表达式 (join1,join2) => new object[] {JoinType.Left,join1.UserNo==join2.UserNo}</param>
        /// <param name="selectExpression">返回表达式 (s1, s2) => new { Id =s1.UserNo, Id1 = s2.UserNo}</param>
        /// <param name="whereLambda">查询表达式 (w1, w2) =>w1.UserNo == "")</param>
        /// <param name="intPageIndex">分页索引</param>
        /// <param name="intPageSize">每页数量</param>
        /// <param name="strOrderByFileds">排序字段</param>
        /// <returns></returns>
        public async Task<PageModel<TResult>> QueryPage<T, T2, T3, T4, TResult>(Expression<Func<T, T2, T3, T4, object[]>> joinExpression,
           Expression<Func<T, T2, T3, T4, TResult>> selectExpression,
           Expression<Func<T, T2, T3, T4, bool>> whereLambda = null, int intPageIndex = 1, int intPageSize = 20, string strOrderByFileds = null)
        {
            RefAsync<int> totalCount = 0;
            List<TResult> list;
            if (whereLambda == null)
            {
                list = await Db.Queryable(joinExpression).OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).Select(selectExpression).ToPageListAsync(intPageIndex, intPageSize, totalCount);
            }
            else
            {
                list = await Db.Queryable(joinExpression).OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds).Where(whereLambda).Select(selectExpression).ToPageListAsync(intPageIndex, intPageSize, totalCount);
            }
            int pageCount = (Math.Ceiling(totalCount.ObjToDecimal() / intPageSize.ObjToDecimal())).ObjToInt();
            return new PageModel<TResult>() { dataCount = totalCount, pageCount = pageCount, page = intPageIndex, PageSize = intPageSize, data = list };
        }

        /// <summary>
        /// 两表联合查询-分页-分组
        /// </summary>
        /// <typeparam name="T">实体1</typeparam>
        /// <typeparam name="T2">实体1</typeparam>
        /// <typeparam name="TResult">返回对象</typeparam>
        /// <param name="joinExpression">关联表达式</param>
        /// <param name="selectExpression">返回表达式</param>
        /// <param name="whereExpression">查询表达式</param>
        /// <param name="groupExpression">分组表达式</param>
        /// <param name="intPageIndex">页码</param>
        /// <param name="intPageSize">页大小</param>
        /// <param name="strOrderByFileds">排序字段</param>
        /// <returns></returns>
        public async Task<PageModel<TResult>> QueryTabsPage<T, T2, TResult>(
            Expression<Func<T, T2, object[]>> joinExpression,
            Expression<Func<T, T2, TResult>> selectExpression,
            Expression<Func<TResult, bool>> whereExpression,
            Expression<Func<T, object>> groupExpression,
            int intPageIndex = 1,
            int intPageSize = 20,
            string strOrderByFileds = null)
        {
            RefAsync<int> totalCount = 0;
            var list = await _db.Queryable<T, T2>(joinExpression).GroupBy(groupExpression)
             .Select(selectExpression)
             .OrderByIF(!string.IsNullOrEmpty(strOrderByFileds), strOrderByFileds)
             .WhereIF(whereExpression != null, whereExpression)
             .ToPageListAsync(intPageIndex, intPageSize, totalCount);
            int pageCount = (Math.Ceiling(totalCount.ObjToDecimal() / intPageSize.ObjToDecimal())).ObjToInt();
            return new PageModel<TResult>() { dataCount = totalCount, pageCount = pageCount, page = intPageIndex, PageSize = intPageSize, data = list };
        }

        #endregion 多表分页查询
    }
}